﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BolDetailBLL
    {
        private static readonly IBolDA BolDA = DataAccess.CreateBolDA();
        private static readonly IBolDetailDA BolDetailDA = DataAccess.CreateBolDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IDeliveryOrderDA DeliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITransportEquipmentDA TransportEquipmentDA = DataAccess.CreateTransportEquipmentDA();

        public BolDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public BolDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BolDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> bolDetailIDs, List<string> lineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<BolDetailInfo> bolDetailInfos = new List<BolDetailInfo>();
                BolDetailInfo bolDetailInfo = null;
                for (i = 0; i < bolDetailIDs.Count; i++)
                {
                    bolDetailInfo = BolDetailDA.GetBolDetailByID(dataBase, tran, bolDetailIDs[i], lineIDs[i], this.dataBaseID[0]);
                    bolDetailInfos.Add(bolDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (BolDetailDA.DeleteBolDetail(dataBase, tran, bolDetailIDs, this.dataBaseID) > 0)
                {
                    for (i = 0; i < bolDetailIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<BolDetailInfo>(dataBase, tran, bolDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "BOL_DETAIL", this.dataBaseID);
                        temp = temp + bolDetailIDs[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return BolDetailDA.GetAllBolDetail(this.dataBaseID[0]);
        }

        public List<BolDetailInfo> GetBolDetailInfoByDeliveryOrderIDsAndEquipmentTypeID(List<string> DeliveryOrderIDs, string EquipmentTypeIDs, string types)
        {
            List<BolDetailInfo> bolDetailInfos = null;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (((DeliveryOrderIDs == null) || (DeliveryOrderIDs.Count <= 0)) || string.IsNullOrEmpty(EquipmentTypeIDs))
                {
                    return bolDetailInfos;
                }
                List<DeliveryOrderInfo> deliveryOrderInfos = new List<DeliveryOrderInfo>();
                DeliveryOrderInfo deliveryOrderInfo = null;
                for (i = 0; i < DeliveryOrderIDs.Count; i++)
                {
                    deliveryOrderInfo = new DeliveryOrderInfo();
                    deliveryOrderInfo = DeliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, DeliveryOrderIDs[i].ToString(), this.dataBaseID[0], this.pubParasInfo);
                    deliveryOrderInfos.Add(deliveryOrderInfo);
                }
                TransportEquipmentInfo transportEquipmentInfo = TransportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, EquipmentTypeIDs, this.dataBaseID[0], this.pubParasInfo);
                if (((deliveryOrderInfos == null) || (deliveryOrderInfos.Count <= 0)) || (transportEquipmentInfo == null))
                {
                    return bolDetailInfos;
                }
                bolDetailInfos = new List<BolDetailInfo>();
                for (i = 0; i < deliveryOrderInfos.Count; i++)
                {
                    BolDetailInfo bolDetailInfo = new BolDetailInfo {
                        BolID = "",
                        ActualEquipment = "",
                        ActualEquipmentType = "",
                        Driver = "",
                        DispatchedBy = "",
                        PlanEquipment = transportEquipmentInfo.TransportEquipmentID,
                        PlanEquipmentType = transportEquipmentInfo.EquipmentTypeID,
                        Carrier = transportEquipmentInfo.CarrierID,
                        DeliveryID = deliveryOrderInfos[i].DeliveryOrderID,
                        TransportType = deliveryOrderInfos[i].TransferType,
                        TransportMode = deliveryOrderInfos[i].DeliveryOrderType
                    };
                    bolDetailInfos.Add(bolDetailInfo);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return bolDetailInfos;
        }

        public List<BolDetailInfo> GetBolDetailInfoListByID(string bolDetailID)
        {
            return BolDetailDA.GetBolDetailListByID(bolDetailID, this.dataBaseID[0]);
        }

        public BolDetailInfo GetInfoByID(string bolDetailID, string lineID)
        {
            return BolDetailDA.GetBolDetailByID(bolDetailID, lineID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(BolDetailQueryEntity bolDetailQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(bolDetailQuery.BolID))
            {
                sqlWhere.Add(" BOL_ID " + bolDetailQuery.BolID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.DeliveryID))
            {
                sqlWhere.Add(" DELIVERY_ID " + bolDetailQuery.BolID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + bolDetailQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + bolDetailQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.DispatchID))
            {
                sqlWhere.Add(" DISPATCH_ID " + bolDetailQuery.DispatchID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.PlanEquipment))
            {
                sqlWhere.Add(" PLAN_EQUIPMENT " + bolDetailQuery.PlanEquipment);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.ActualEquipment))
            {
                sqlWhere.Add(" ACTUAL_EQUIPMENT_TYPE " + bolDetailQuery.ActualEquipment);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.Driver))
            {
                sqlWhere.Add(" DRIVER " + bolDetailQuery.Driver);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.Carrier))
            {
                sqlWhere.Add(" CARRIER " + bolDetailQuery.Carrier);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.DispatchedBy))
            {
                sqlWhere.Add(" DISPATCHED_BY " + bolDetailQuery.DispatchedBy);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.TransportMode))
            {
                sqlWhere.Add(" TRANSPORT_MODE " + bolDetailQuery.TransportMode);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.TransportType))
            {
                sqlWhere.Add(" TRANSPORT_TYPE " + bolDetailQuery.TransportType);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.Qty))
            {
                sqlWhere.Add(" QTY " + bolDetailQuery.Qty);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.PackID))
            {
                sqlWhere.Add(" PACK_ID " + bolDetailQuery.PackID);
            }
            if (!string.IsNullOrEmpty(bolDetailQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + bolDetailQuery.IsActive);
            }
            return BolDetailDA.GetBolDetailByQueryList(sqlWhere, bolDetailQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string bolDetailID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                BolDetailInfo bolDetailInfo = obj as BolDetailInfo;
                if (!string.IsNullOrEmpty(bolDetailInfo.BolID))
                {
                    if (BolDetailDA.CheckBolDetailIDUnique(dataBase, tran, bolDetailInfo.BolID, bolDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F8200_001", this.pubParasInfo.UserLanguage, new List<string> { bolDetailInfo.BolID });
                    }
                }
                else
                {
                    bolDetailInfo.BolID = commonMethod.GenNextNumStr(dataBase, tran, "BOL_DETAIL", this.dataBaseID[0]);
                }
                bolDetailInfo.UpdatedBy = bolDetailInfo.CreatedBy;
                bolDetailInfo.CreatedDate = DateTime.Now;
                bolDetailInfo.UpdatedDate = DateTime.Now;
                if (BolDetailDA.InsertBolDetail(dataBase, tran, bolDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BolDetailInfo>(dataBase, tran, null, bolDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "BOL_DETAIL", this.dataBaseID[0]);
                    bolDetailID = bolDetailInfo.BolID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return bolDetailID;
        }

        public string InsertBolDetailList(List<BolDetailInfo> bolDetailInfos)
        {
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                {
                    List<string> oldBolIDs = new List<string>();
                    Dictionary<string, string> actualEquipmentList = new Dictionary<string, string>();
                    int i = 0;
                    while (i < bolDetailInfos.Count)
                    {
                        if (!(string.IsNullOrEmpty(bolDetailInfos[i].BolID) || oldBolIDs.Contains(bolDetailInfos[i].BolID)))
                        {
                            oldBolIDs.Add(bolDetailInfos[i].BolID);
                        }
                        if (!actualEquipmentList.ContainsKey(bolDetailInfos[i].ActualEquipment))
                        {
                            string bolID = commonMethod.GenNextNumStr(dataBase, tran, "BOL_DETAIL", whLoginID);
                            actualEquipmentList.Add(bolDetailInfos[i].ActualEquipment, bolID);
                        }
                        i++;
                    }
                    if ((oldBolIDs != null) && (oldBolIDs.Count > 0))
                    {
                        BolDetailDA.DeleteBolDetail(dataBase, tran, oldBolIDs, this.dataBaseID);
                    }
                    foreach (KeyValuePair<string, string> item in actualEquipmentList)
                    {
                        int lineID = 1;
                        for (i = 0; i < bolDetailInfos.Count; i++)
                        {
                            if (item.Key == bolDetailInfos[i].ActualEquipment)
                            {
                                bolDetailInfos[i].BolID = item.Value;
                                bolDetailInfos[i].LineID = lineID * 10;
                                bolDetailInfos[i].Status = BolDetailStatus.NEW;
                                lineID++;
                                BolDetailDA.InsertBolDetail(dataBase, tran, bolDetailInfos[i], this.dataBaseID);
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string InsertBolDetailList(DataBase dataBase, DbTransaction tran, List<BolDetailInfo> bolDetailInfos)
        {
            string temp = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DbConnection conn = dataBase.connection;
            List<BolInfo> bolInfos = new List<BolInfo>();
            if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
            {
                List<string> oldBolIDs = new List<string>();
                Dictionary<string, string> actualEquipmentList = new Dictionary<string, string>();
                int i = 0;
                while (i < bolDetailInfos.Count)
                {
                    if (!(string.IsNullOrEmpty(bolDetailInfos[i].BolID) || oldBolIDs.Contains(bolDetailInfos[i].BolID)))
                    {
                        oldBolIDs.Add(bolDetailInfos[i].BolID);
                    }
                    if (!actualEquipmentList.ContainsKey(bolDetailInfos[i].ActualEquipment))
                    {
                        string bolID = commonMethod.GenNextNumStr(dataBase, tran, "BOL_DETAIL", whLoginID);
                        actualEquipmentList.Add(bolDetailInfos[i].ActualEquipment, bolID);
                    }
                    i++;
                }
                if ((oldBolIDs != null) && (oldBolIDs.Count > 0))
                {
                    BolDA.DeleteBol(dataBase, tran, oldBolIDs, this.dataBaseID);
                    BolDetailDA.DeleteBolDetail(dataBase, tran, oldBolIDs, this.dataBaseID);
                }
                foreach (KeyValuePair<string, string> item in actualEquipmentList)
                {
                    int lineID = 1;
                    BolInfo bolInfo = new BolInfo {
                        BolID = item.Value,
                        TransportEquipmentID = item.Key,
                        DriverID = "",
                        Status = BolDetailStatus.NEW,
                        Qty = 0M,
                        GrossWeight = 0M,
                        NetWeight = 0M,
                        Cube = 0M,
                        FreightSelf = 0M,
                        FreightDealer = 0M,
                        Paid = "N",
                        Udf1 = "",
                        Udf2 = "",
                        Udf3 = "",
                        Udf4 = "",
                        Udf5 = "",
                        Udf6 = "",
                        Udf7 = "",
                        Udf8 = "",
                        Udf9 = "",
                        Udf10 = "",
                        Remark = "",
                        IsActive = "Y",
                        CreatedBy = this.pubParasInfo.UserID,
                        CreatedDate = DateTime.Now,
                        UpdatedBy = this.pubParasInfo.UserID,
                        UpdatedDate = DateTime.Now
                    };
                    BolDA.InsertBol(dataBase, tran, bolInfo, this.dataBaseID);
                    for (i = 0; i < bolDetailInfos.Count; i++)
                    {
                        if (item.Key == bolDetailInfos[i].ActualEquipment)
                        {
                            bolDetailInfos[i].BolID = item.Value;
                            bolDetailInfos[i].LineID = lineID * 10;
                            bolDetailInfos[i].Status = BolDetailStatus.NEW;
                            lineID++;
                            temp = temp + BolDetailDA.InsertBolDetail(dataBase, tran, bolDetailInfos[i], this.dataBaseID) + ",";
                        }
                    }
                }
            }
            if ((temp.Length > 0) && temp.EndsWith(","))
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return temp;
        }

        public string Update(object obj)
        {
            string bolDetailID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                BolDetailInfo bolDetailInfo = obj as BolDetailInfo;
                bolDetailInfo.UpdatedDate = DateTime.Now;
                BolDetailInfo oldBolDetailInfo = BolDetailDA.GetBolDetailByID(dataBase, tran, bolDetailInfo.BolID, bolDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (BolDetailDA.UpdateBolDetail(bolDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BolDetailInfo>(oldBolDetailInfo, bolDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "BOL_DETAIL", this.dataBaseID[0]);
                    bolDetailID = bolDetailInfo.BolID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return bolDetailID;
        }

        public string UpdateBolDetailList(List<BolDetailInfo> bolDetailInfos)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                DeliveryOrderInfo deliveryOrderInfo;
                List<string> deliveryOrderIDs = null;
                List<string> dispatchIDs = null;
                List<string> driverIDs = null;
                List<string> transportEquimentIDs = null;
                List<string> bolIDs = new List<string>();
                if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                {
                    dispatchIDs = new List<string>();
                    driverIDs = new List<string>();
                    transportEquimentIDs = new List<string>();
                    deliveryOrderIDs = new List<string>();
                    IDeliveryOrderDetailDA deliveryOrderDetailDA = DataAccess.CreateDeliveryOrderDetailDA();
                    for (i = 0; i < bolDetailInfos.Count; i++)
                    {
                        if (!bolIDs.Contains(bolDetailInfos[i].BolID))
                        {
                            bolIDs.Add(bolDetailInfos[i].BolID);
                        }
                        DeliveryOrderDetailInfo deliveryOrderDetailInfo = new DeliveryOrderDetailInfo();
                        if (!deliveryOrderIDs.Contains(bolDetailInfos[i].DeliveryID))
                        {
                            deliveryOrderIDs.Add(bolDetailInfos[i].DeliveryID);
                        }
                        if (!dispatchIDs.Contains(bolDetailInfos[i].DispatchID))
                        {
                            dispatchIDs.Add(bolDetailInfos[i].DispatchID);
                        }
                        if (!driverIDs.Contains(bolDetailInfos[i].Driver))
                        {
                            driverIDs.Add(bolDetailInfos[i].Driver);
                        }
                        if (!transportEquimentIDs.Contains(bolDetailInfos[i].ActualEquipment))
                        {
                            transportEquimentIDs.Add(bolDetailInfos[i].ActualEquipment);
                        }
                        deliveryOrderDetailInfo = deliveryOrderDetailDA.GetDeliveryOrderDetailByID(dataBase, tran, bolDetailInfos[i].DeliveryID, bolDetailInfos[i].LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                        if (deliveryOrderDetailInfo != null)
                        {
                            deliveryOrderDetailInfo.Status = DeliveryOrderStatus.DISPATCHED;
                            deliveryOrderDetailDA.UpdateDeliveryOrderDetail(dataBase, tran, deliveryOrderDetailInfo, this.dataBaseID);
                        }
                        BolDetailInfo bolDetailInfo = bolDetailInfos[i];
                        bolDetailInfo.Status = BolDetailStatus.ONTHEWAY;
                        BolDetailDA.UpdateBolDetail(dataBase, tran, bolDetailInfo, this.dataBaseID);
                    }
                }
                foreach (string bolID in bolIDs)
                {
                    BolInfo bolInfo = BolDA.GetBolByID(dataBase, tran, bolID, this.dataBaseID[0]);
                    bolInfo.Status = BolDetailStatus.ONTHEWAY;
                    bolInfo.UpdatedBy = this.pubParasInfo.UserID;
                    BolDA.UpdateBol(dataBase, tran, bolInfo, this.dataBaseID);
                }
                if ((deliveryOrderIDs != null) && (deliveryOrderIDs.Count > 0))
                {
                    IDeliveryOrderDA deliveryOrderDA = DataAccess.CreateDeliveryOrderDA();
                    for (i = 0; i < deliveryOrderIDs.Count; i++)
                    {
                        deliveryOrderInfo = new DeliveryOrderInfo();
                        deliveryOrderInfo = deliveryOrderDA.GetDeliveryOrderByID(dataBase, tran, deliveryOrderIDs[i], this.dataBaseID[0], this.pubParasInfo);
                        if (deliveryOrderInfo != null)
                        {
                            deliveryOrderInfo.Status = DeliveryOrderStatus.DISPATCHED;
                            deliveryOrderDA.UpdateDeliveryOrder(dataBase, tran, deliveryOrderInfo, this.dataBaseID);
                        }
                    }
                }
                if ((dispatchIDs != null) && (dispatchIDs.Count > 0))
                {
                    IDispatchDA dispatchDA = DataAccess.CreateDispatchDA();
                    for (i = 0; i < dispatchIDs.Count; i++)
                    {
                        DispatchInfo dispatchInfo = new DispatchInfo();
                        dispatchInfo = dispatchDA.GetDispatchByID(dataBase, tran, dispatchIDs[i], this.dataBaseID[0], this.pubParasInfo);
                        if (dispatchInfo != null)
                        {
                            dispatchInfo.Status = DispatchStatus.COMPLETED;
                            dispatchDA.UpdateDispatch(dataBase, tran, dispatchInfo, this.dataBaseID);
                            temp = temp + dispatchIDs[i] + ",";
                        }
                    }
                }
                if ((driverIDs != null) && (driverIDs.Count > 0))
                {
                    IDriverDA driverDA = DataAccess.CreateDriverDA();
                    for (i = 0; i < driverIDs.Count; i++)
                    {
                        DriverInfo driverInfo = new DriverInfo();
                        driverInfo = driverDA.GetDriverByID(dataBase, tran, driverIDs[i], this.dataBaseID[0], this.pubParasInfo);
                        if (driverInfo != null)
                        {
                            driverInfo.Status = DriverStatus.ONTHEWAY;
                            driverDA.UpdateDriver(dataBase, tran, driverInfo, this.dataBaseID);
                        }
                    }
                }
                if ((transportEquimentIDs != null) && (transportEquimentIDs.Count > 0))
                {
                    ITransportEquipmentDA transportEquipmentDA = DataAccess.CreateTransportEquipmentDA();
                    for (i = 0; i < transportEquimentIDs.Count; i++)
                    {
                        TransportEquipmentInfo transportEquipmentInfo = new TransportEquipmentInfo();
                        transportEquipmentInfo = transportEquipmentDA.GetTransportEquipmentByID(dataBase, tran, transportEquimentIDs[i], this.dataBaseID[0], this.pubParasInfo);
                        if (transportEquipmentInfo != null)
                        {
                            transportEquipmentInfo.Status = TransportEquipmentStatus.ONTHEWAY;
                            transportEquipmentDA.UpdateTransportEquipment(dataBase, tran, transportEquipmentInfo, this.dataBaseID);
                        }
                    }
                }
                List<string> BolID = new List<string>();
                IDeliveryOrderDA deliveryOrderDa = DataAccess.CreateDeliveryOrderDA();
                for (i = 0; i < bolDetailInfos.Count; i++)
                {
                    if (!BolID.Contains(bolDetailInfos[i].BolID))
                    {
                        BolID.Add(bolDetailInfos[i].BolID);
                        string strPodID = commonMethod.GenNextNumStr(dataBase, tran, "POD", this.dataBaseID[0]);
                        PodInfo podInfo = new PodInfo {
                            PodID = strPodID,
                            BolID = bolDetailInfos[i].BolID,
                            DeliveryID = bolDetailInfos[i].DeliveryID,
                            Status = PodStatus.NEW,
                            CarrierID = bolDetailInfos[i].Carrier,
                            OwnerID = bolDetailInfos[i].OwnerID,
                            EquipmentID = bolDetailInfos[i].ActualEquipment,
                            DriverID = bolDetailInfos[i].Driver,
                            TransportMode = bolDetailInfos[i].TransportMode
                        };
                        deliveryOrderInfo = deliveryOrderDa.GetDeliveryOrderByID(dataBase, tran, bolDetailInfos[i].DeliveryID, this.dataBaseID[0], this.pubParasInfo);
                        podInfo.ExceptArriverdDate = deliveryOrderInfo.ExpectArrivalDate;
                        podInfo.CustomerID = deliveryOrderInfo.CustomerID;
                        podInfo.CustomerName = deliveryOrderInfo.CName;
                        podInfo.Contact = deliveryOrderInfo.CContact1;
                        podInfo.CPhone = deliveryOrderInfo.CTel1;
                        podInfo.CAddress = deliveryOrderInfo.CAddress;
                        podInfo.PickupType = deliveryOrderInfo.PickupType;
                        podInfo.CreatedBy = this.pubParasInfo.UserID;
                        podInfo.UpdatedBy = this.pubParasInfo.UserID;
                        podInfo.CreatedDate = DateTime.Now;
                        podInfo.UpdatedDate = DateTime.Now;
                        DataAccess.CreatePodDA().InsertPod(dataBase, tran, podInfo, this.dataBaseID);
                        List<BolDetailInfo> tempBolDetail = new List<BolDetailInfo>();
                        tempBolDetail = BolDetailDA.GetBolDetailListByID(dataBase, tran, bolDetailInfos[i].BolID, this.dataBaseID[0]);
                        if ((tempBolDetail != null) && (tempBolDetail.Count > 0))
                        {
                            for (int j = 0; j < tempBolDetail.Count; j++)
                            {
                                PodDetailInfo podDetailInfo = new PodDetailInfo {
                                    PodID = strPodID,
                                    LineID = (j + 1) * 10,
                                    BolLineID = tempBolDetail[j].LineID,
                                    DeliveryID = tempBolDetail[j].DeliveryID,
                                    DeliveryLineID = tempBolDetail[j].DeliveryLineID,
                                    Status = PodStatus.NEW,
                                    OwnerID = tempBolDetail[j].OwnerID,
                                    SkuID = tempBolDetail[j].SkuID,
                                    PackID = tempBolDetail[j].PackID,
                                    Qty = tempBolDetail[j].Qty,
                                    AcceptedQty = 0.0M,
                                    RejectedQty = 0.0M,
                                    LoseQty = 0.0M,
                                    BreakageQty = 0.0M,
                                    PkgBreakageQty = 0.0M,
                                    CreatedBy = this.pubParasInfo.UserID,
                                    UpdatedBy = this.pubParasInfo.UserID,
                                    CreatedDate = DateTime.Now,
                                    UpdatedDate = DateTime.Now
                                };
                                DataAccess.CreatePodDetailDA().InsertPodDetail(dataBase, tran, podDetailInfo, this.dataBaseID);
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return (temp = temp.Substring(0, temp.Length - 1));
            }
            return "";
        }
    }
}

